package com.lyh.test;

import com.lyh.test.dto.Test;
import com.lyh.test.dto.UserInfo;
import com.lyh.test.mapper.TestMapper;
import com.lyh.test.mapper.UserInfoMapper;
import com.lyh.test.utils.TreePlusTable;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        //获取核心配置文件的输入流
        try {
            InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
            //获取SqlSessionFactoryBuilder对象
            SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
            //获取sql的会话对象
            SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
            //获取sql的会话对象SqlSession(不会自动提交事务)，是MyBatis提供的操作数据库的对象
            //SqlSession sqlSession = sqlSessionFactory.openSession();
            //获取sql的会话对象SqlSession(会自动提交事务)，是MyBatis提供的操作数据库的对象
            SqlSession sqlSession = sqlSessionFactory.openSession(true);
            //获取UserMapper的代理实现类对象
            TestMapper testMapper = sqlSession.getMapper(TestMapper.class);
            //调用mapper接口中的方法，实现添加用户的信息的功能
            List<Test> tests = testMapper.selectTestAll();
            //提供sql以及的唯一标识找到sql并执行，唯一标识是namespace.sqlId
            //int result = sqlSession.insert("com.atguigu.mybatis.mapper.UserMapper.insertUser");
            //System.out.println("结果：" + tests);
            long startDt = System.currentTimeMillis();

            TreePlusTable<Integer, Test> treePlusTable = new TreePlusTable<>(15);
            for (Test test : tests) {
                treePlusTable.put(test.getId(), test);
            }
            long endDt = System.currentTimeMillis();
            System.out.println((endDt - startDt)+ "s");
            System.out.println(treePlusTable);
            //提交事务
            //sqlSession.commit();
            //关闭SqlSession
            sqlSession.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
